package com.dyz.leetcode._18_backtracking;

import java.util.ArrayList;
import java.util.List;

public class _22_generateParenthesis {
    public List<String> generateParenthesis(int n) {
        List<String> res = new ArrayList<>();
        if(n<=0){return res;}
        dfs(n, "", res, 0,0);
        return res;

    }
    private void dfs(int n, String path, List<String> res, int open, int close){
        if(open>n || close>open){return;}
        if(path.length()==2*n){
            res.add(path);
            return;
        }

        dfs(n, path+"(",res, open+1, close);
        dfs(n, path+")",res, open, close+1);
    }
}
